from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser
from smart.utils.models import BaseModel


# 用户表(学生 老师 教师)

# admin账号密码：  superuser
class UserInfo(AbstractUser):
    """用户模型类"""
    # 手机号
    telephone = models.CharField(max_length=11, unique=True, verbose_name='手机号码', null=True, blank=True)

    class Meta:
        db_table = 'smart_userinfo'  # 创建的表名，如果不写，默认app_userinfo
        # 下面两个是配套的，用来在admin中表名显示汉字
        verbose_name = '用户信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


# 课程表
class Home(BaseModel):
    """
    课程
    """
    name = models.CharField(max_length=225, verbose_name="房间名称")

    # 课程照片
    home_img = models.ImageField(upload_to="home", verbose_name="封面图片", default='home/n1.jpg')
    userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, db_constraint=False,
                                 related_name='h_u')

    class Meta:
        db_table = "smart_home"
        verbose_name = "房间"
        verbose_name_plural = "房间"

    def __str__(self):
        return self.name


class Equipment(BaseModel):
    """
    课程
    """
    name_choice = (
        (1, '灯'),
        (2, '开关'),
        (3, '传感器'),
        (4, '门锁')
    )
    name = models.BigIntegerField(verbose_name="设备", choices=name_choice)
    desc = models.BigIntegerField(verbose_name='自定义', default=0, help_text='用于显示温度/灯亮度...')
    # 课程照片
    equ_img = models.ImageField(upload_to="equ", verbose_name="封面图片", default='b_9.jpg')
    home = models.ForeignKey(verbose_name='房间', to='Home', on_delete=models.DO_NOTHING, db_constraint=False,
                             related_name='e_h')

    class Meta:
        db_table = "smart_equipment"
        verbose_name = "智能设备"
        verbose_name_plural = "智能设备"

    def __str__(self):
        return self.name


class Record(models.Model):
    """
    上报记录
    """
    equ = models.ForeignKey(verbose_name='设备', to='Equipment', on_delete=models.DO_NOTHING, db_constraint=False,
                            related_name='r_e', null=True, blank=True)
    home = models.ForeignKey(verbose_name='房间', to='Home', on_delete=models.DO_NOTHING, db_constraint=False,
                             related_name='r_h', null=True, blank=True)
    desc = models.CharField(max_length=225, verbose_name="上报原因", null=True, blank=True)
